package start.demo01.datatype.json.entity;

import lombok.Data;

@Data
public class ConfigInfo {

    private TableInfo tableInfo;
    private ESInfo esInfo;
    private KafkaInfo kafkaInfo;
    private SyslogInfo syslogInfo;

    public void build(String info){
        int index = info.indexOf("table");
        if(index!=-1) {
            String args = info.substring(info.indexOf("(",index)+1,
                    info.indexOf(")",index));
            String[] split = args.split(",");
            tableInfo=new ConfigInfo.TableInfo(split[0]);
        }
        index=info.indexOf("ES");
        if(index!=-1) {
            String args = info.substring(info.indexOf("(",index)+1,
                    info.indexOf(")",index));
            String[] split = args.split(",");
            esInfo=new ConfigInfo.ESInfo(split[0]);
        }
        index=info.indexOf("kafka");
        if(index!=-1) {
            String args = info.substring(info.indexOf("(",index)+1,
                    info.indexOf(")",index));
            kafkaInfo=new ConfigInfo.KafkaInfo(args);
        }
        index=info.indexOf("syslog");
        if(index!=-1) {
            String args = info.substring(info.indexOf("(",index)+1,
                    info.indexOf(")",index));
            syslogInfo=new ConfigInfo.SyslogInfo(args);
        }
    }


    @Data
    public static class TableInfo{
        private String name;
        private String userName;
        private String password;

        public TableInfo(String name) {
            this.name = name;
        }
    }
    @Data
    public static class ESInfo{
        private String index;
        public ESInfo(String index) {
            this.index = index;
        }
    }
    @Data
    public static class KafkaInfo{
        private String servers;
        private String topic;
        private String key;

        public KafkaInfo(String args) {
            String[] split = args.split(",");
            servers=split[0];
            topic=split[1];
            if(split.length==3)
                key=split[2];
        }
    }
    @Data
    public static class SyslogInfo{
        private String protocol;
        private String ip;
        private Integer port;
        private Integer level=2;

        public SyslogInfo(String args) {
            String[] split = args.split(",");
            this.protocol=split[0];
            this.ip = split[1];
            this.port=Integer.parseInt(split[2]);
            if(split.length>3)
                level=Integer.parseInt(split[3]);
        }
    }


}
